/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
Ext.chart.Chart.CHART_URL = '../../../ext3/resources/charts.swf';
Ext.QuickTips.init();
var _pageSize = 20;
var _title = '监测点日数据';
var store;
var now = new Date();
var loadServlet = '../../../servlet/data/RsjLoad';

var tabs = new Ext.TabPanel({
    activeTab: 0,
    plain: true,
    enableTabScroll: true,
    defaults: {autoScroll: true},
    items: [{
            title: currentOrgName + _title,
            id: 'main-tab',
            layout: "fit",
            tbar: new Ext.Toolbar([
                {
                    text: '锁定',
                    iconCls: 'icon-lock',
                    //hidden:'true',
                    handler: lock
                            , style: {marginLeft: '10px'}
                },
                {
                    text: '解锁',
                    iconCls: 'icon-unlock',
                    handler: unlock
                }
            ]),
            listeners: {
                'render': function() {
                    tbarPoint.render(this.tbar);
                }
            }
        }
    ]
});

function lock() {
    var records = checkboxSel.getSelections();
    if (records.length === 0) {
        Ext.Msg.alert("系统提示", "您还没有选择数据");
        return;
    }
    var pointIds = getValidFields(records, "pointId",'1');
    if (pointIds === '') {
        Ext.Msg.alert("系统提示", "没有有效数据");
        return;
    }
    
    var date = Ext.getCmp("dateFrom").getValue().format('Ymd');
    var token1 = new Date().getTime();
    Ext.Ajax.request({
        url: '../../../servlet/data/LockServlet',
        success: function(response, options) {
            store.reload();
        },
        failure: function(response, opts) {
            Ext.Msg.alert('系统提示', '同步失败，清稍候再试 ' + response.status);
        },
        params: {token:token,token1:token1,cmd: "lockRPoints", pointIds: pointIds, date: date}
    });
}

function unlock() {
    var records = checkboxSel.getSelections();
    if (records.length === 0) {
        Ext.Msg.alert("系统提示", "您还没有选择数据");
        return;
    }
var pointIds = getValidFields(records, "pointId",'0');
    if (pointIds === '') {
        Ext.Msg.alert("系统提示", "没有有效数据");
        return;
    }
    
    var date = Ext.getCmp("dateFrom").getValue().format('Ymd');
    Ext.Ajax.request({
        url: '../../../servlet/data/LockServlet',
        success: function(response, options) {
            store.reload();
        },
        failure: function(response, opts) {
            Ext.Msg.alert('系统提示', '同步失败，清稍候再试 ' + response.status);
        },
        params: {cmd: "unlockRPoints", pointIds: pointIds, date: date}
    });
}

var checkboxSel = new Ext.grid.CheckboxSelectionModel();
var store;
function buildWindow() {
    store = new Ext.data.JsonStore({
        url: loadServlet,
        root: 'data',
        idProperty: 'id',
        totalProperty: 'count',
        fields: fieldName,
        autoLoad: true,
        baseParams: {cmd: 'lock', start: 0, limit: _pageSize, beginDate: now.add(Date.DATE, -1).format('Ymd'), endDate: now.add(Date.DATE, -1).format('Ymd')}
    });

    tbarPoint.insert(0, {
        xtype: 'datefield',
        id: 'dateFrom',
        format: 'Y年m月d日',
        width: 150,
        value: now.add(Date.DATE, -1),
        listeners: {
            select: function(c, date) {
                beginDate = date.format('Ymd');
                store.baseParams.beginDate = beginDate;
                store.baseParams.endDate = beginDate;
            }
        }
    }
    );

    var grid = new Ext.grid.GridPanel({
        //title: _title,
        store: store,
        sm: checkboxSel,
        columns: [
            checkboxSel,
            {header: "状态", dataIndex: 'yStatus', width: 40,
                renderer: function renderer(v, metaData, record, rowIndex, colIndex, store) {
                    var value = parseInt(v);
                    var imageName;
                    if (value === 1) {
                        imageName = 'lock.png';
                        return "<image src='" + basePath + "image/icon/" + imageName + "'/>";
                    } else
                        return "";
                }
            }
        ].concat(columns),
        loadMask: true,
        bbar: new Ext.PagingToolbar({
            pageSize: _pageSize,
            store: store,
            displayInfo: true,
            displayMsg: '显示记录：{0} - {1}   总记录：{2}',
            emptyMsg: '目前没有信息'
        })
    });
    Ext.getCmp('main-tab').add(grid);
    var applicationUI = new Ext.Viewport({
        layout: "fit",
        items: [
            tabs
        ]
    });
}
Ext.onReady(buildWindow);
